package com.htu.edu.controller;

import com.htu.edu.bean.Cart;
import com.htu.edu.common.R;
import com.htu.edu.service.CartService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
// 2. 设置该模块的请求路径，如/user
@RequestMapping("/Cart")
// 3. 解决跨域
@CrossOrigin
public class CartController {
    @Autowired
    private CartService cartService;
    @PostMapping("/listCart")
    public R listCart(Cart cart){
        try {
            List<Cart> listCart = cartService.listCart(cart);
            return R.ok().data(listCart);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }
    @PostMapping("/saveCart")
    public R saveCart(Cart cart){
        try {
            cartService.saveCart(cart);
            return R.ok().data(1);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }
    @PostMapping("/updateCart")
    public R updateCart(Cart cart){
        try {
            cartService.updateCart(cart);
            return R.ok().data(1);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }
    @PostMapping("/removeCart")
    public R removeCart(Cart cart){
        try {
             cartService.removeCart(cart);
             return R.ok().data(1);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }
}
